<template>
  <div class="app-container">
    <!-- 当前课程信息显示区域 -->
    <el-card v-if="currentCourseInfo" class="mb-20" shadow="never">
      <div class="course-header">
        <el-row :gutter="20" align="middle">
          <el-col :span="18">
            <div class="course-title">
              <i class="el-icon-back" @click="goBack" style="cursor: pointer; margin-right: 10px; color: #409EFF;"></i>
              <span class="title-text">课程章节管理</span>
              <el-divider direction="vertical"></el-divider>
              <el-tag type="primary" size="medium">{{ currentCourseInfo.courseName }}</el-tag>
            </div>
            <div class="course-subtitle">
              <span class="subtitle-text">当前查看：{{ currentCourseInfo.courseName }} 的章节列表</span>
            </div>
          </el-col>
          <el-col :span="6" style="text-align: right;">
            <el-button type="primary" icon="el-icon-back" size="small" @click="goBack">
              返回课程列表
            </el-button>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="章节名称" prop="chapterName">
        <el-input
          v-model="queryParams.chapterName"
          placeholder="请输入章节名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="章节描述" prop="chapterDesc">
        <el-input
          v-model="queryParams.chapterDesc"
          placeholder="请输入章节描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="chapterStatus">
        <el-select
          v-model="queryParams.chapterStatus"
          placeholder="请选择状态"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="正常" :value="1"></el-option>
          <el-option label="删除" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['courseManagement:chapter:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['courseManagement:chapter:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['courseManagement:chapter:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['courseManagement:chapter:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="chapterList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="章节id" align="center" prop="chapterId" />
      <el-table-column label="章节名称" align="center" prop="chapterName" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="章节描述" align="center" prop="chapterDesc" />
      <el-table-column label="排序" align="center" prop="chapterSort" />
      <el-table-column label="状态" align="center" prop="chapterStatus">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.chapterStatus == 1"
            >正常</el-tag
          >
          <el-tag type="danger" v-else>删除</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
        <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleList(scope.row)"
            v-hasPermi="['courseManagement:chapter:edit']"
            >课件详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['courseManagement:chapter:edit']"
            >视频详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['courseManagement:chapter:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['courseManagement:chapter:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程章节对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属课程" prop="courseName">
          <el-select
            v-model="form.courseId"
            clearable
            placeholder="请输入课程名称"
          >
            <el-option
              v-for="item in courseList"
              :key="item.courseId"
              :label="item.courseName"
              :value="item.courseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="章节名称" prop="chapterName">
          <el-input v-model="form.chapterName" placeholder="请输入章节名称" />
        </el-form-item>
        <el-form-item label="章节描述" prop="chapterDesc">
          <el-input v-model="form.chapterDesc" placeholder="请输入章节描述" />
        </el-form-item>
        <el-form-item label="排序" prop="chapterSort">
          <el-input v-model="form.chapterSort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="chapterStatus">
          <el-radio-group v-model="form.chapterStatus">
            <el-radio :label="0">删除</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChapter,
  getChapter,
  delChapter,
  addChapter,
  updateChapter,
} 
from "@/api/courseManagement/chapter";
import Cookies from "js-cookie";
import { listCourse } from "@/api/courseManagement/course";

export default {
  name: "Chapter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程章节表格数据
      chapterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //校区id
      campusId:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chapterName: null,
        courseName: null,
        campusId: null,
        courseId: null,
        chapterDesc: null, 
        chapterStatus: null,
      },
      // 表单参数
      form: {
        chapterStatus: 1,
        campusId:null,
      },
      // 表单校验
      rules: {},
      // 课程列表
      courseList: [],
      // 当前课程信息
      currentCourseInfo: null,
    };
  },
  created() {
    const user= JSON.parse(Cookies.get("user"))
    this.campusId = user.campusId;
    this.form.campusId = this.campusId;
    this.queryParams.campusId = this.campusId;
    // 接收从课程页面传递过来的参数
    this.receiveParams();
    this.getList();
  },
  methods: {
    
    // 接收传递的参数
    receiveParams() {
      this.reset();
      const courseId = this.$route.query.courseId;
      const courseName = this.$route.query.courseName;
      // 如果有传递课程ID，则设置到查询参数中
      if (courseId) {
        this.queryParams.courseId = courseId;
        // 如果有课程名称，也设置到查询参数中
        if (courseName) {
          this.queryParams.courseName = courseName;
        }
        
        // 设置当前课程信息
        this.currentCourseInfo = {
          courseId: courseId,
          courseName: courseName || '未知课程'
        };
        
        // 显示消息提醒
        this.$message({
          message: `已切换到课程：${courseName} 的章节管理`,
          type: 'success',
          duration: 3000,
          showClose: true
        });
      }
    },
    /** 查询课程章节列表 */
    getList() {
      this.loading = true;
      listChapter(this.queryParams).then((response) => {
        this.chapterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询课程列表 */
    getCourseList() {
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chapterId: null,
        chapterName: null,
        courseId: null,
        chapterDesc: null,
        chapterSort: null,
        chapterStatus: 1,
        createTime: null,
        updateTime: null,
        campusId:this.campusId,
        demo1: null,
        demo2: null,
        demo3: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.chapterId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getCourseList();
      this.open = true;
      this.title = "添加课程章节";
    },
    /** 此章节下的课件详情 */
    handleList(row){
      //跳转到课件页面，并且将参数章节名称chapterName传递过去
      this.$router.push({
        path: '/courseManagement/courseware',
        query: {
          chapterId: row.chapterId,
          chapterName: row.chapterName
        }
      });
    },
    /** 此章节下的视频详情 */
    handleView(row){
      //跳转到视频页面，并且将参数章节名称chapterName传递过去
      this.$router.push({
        path: '/courseManagement/video',
        query: {
          chapterId: row.chapterId,
          chapterName: row.chapterName
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getCourseList();
      const chapterId = row.chapterId || this.ids;
      getChapter(chapterId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程章节";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.chapterId != null) {
            updateChapter(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chapterIds = row.chapterId || this.ids;
      this.$modal
        .confirm('是否确认删除课程章节编号为"' + chapterIds + '"的数据项？')
        .then(function () {
          return delChapter(chapterIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "courseManagement/chapter/export",
        {
          ...this.queryParams,
        },
        `chapter_${new Date().getTime()}.xlsx`
      );
    },
    // 返回课程列表
    goBack() {
      this.$router.push('/courseManagement/course');
    },
  },
};
</script>
<style>
.el-form-item__label{
  font-size:13px
}

.course-info {
  padding: 10px 0;
}

.info-label {
  font-weight: bold;
  color: #606266;
  margin-right: 10px;
}

.info-value {
  color: #409EFF;
  font-weight: 500;
}

.mb-20 {
  margin-bottom: 20px;
}

.course-header {
  padding: 10px 0;
}

.course-title {
  display: flex;
  align-items: center;
}

.title-text {
  font-size: 18px;
  font-weight: bold;
  margin-right: 10px;
}

.course-subtitle {
  margin-top: 5px;
}

.subtitle-text {
  color: #606266;
  font-weight: 500;
}
</style>
